﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using BDConexion;
using Entidades;

namespace CapaDatos
{
   public class CargoDAO
    {

       public conexionBD conn = new conexionBD();
        private SqlCommand cmdCargo = new SqlCommand();

        public string IngresarUsuario(Cargo c)
        {
            string rpta = "";
            try
            {
                cmdCargo.CommandType = CommandType.StoredProcedure;
                cmdCargo.CommandText = "SP_Insertar_Cargo";
                cmdCargo.Connection = conn.conectarBD();
                {
                    cmdCargo.Parameters.AddWithValue("@pIdCargo", c.IdCargo);
                    cmdCargo.Parameters.AddWithValue("@pNombre", c.Nombre);
                    cmdCargo.Parameters.AddWithValue("@pDescripcion", c.Descripcion);



                }
                int registros;
                registros = cmdCargo.ExecuteNonQuery();
                
                if (registros == 1)
                {
                    rpta = "OK";
                }
                else
                {
                    rpta = "Error al Insertar Cargo";
                }


            }
            catch (Exception ex)
            {

                System.Console.Write(ex.Message);
            }
            return rpta;
        }

        public string actualizarCargo(Cargo c)
        {
            string rpta = "";
            try
            {
                cmdCargo.CommandType = CommandType.StoredProcedure;
                cmdCargo.CommandText = "SP_Actualizar_Cargo";
                cmdCargo.Connection = conn.conectarBD();
                {
                    cmdCargo.Parameters.AddWithValue("@pIdCargo", c.IdCargo);
                    cmdCargo.Parameters.AddWithValue("@pNombre", c.Nombre);
                    cmdCargo.Parameters.AddWithValue("@pDescripcion", c.Descripcion);
                }
                int registros;
                registros = cmdCargo.ExecuteNonQuery();
                if (registros == 1)
                {
                    rpta = "OK";

                }
                else
                {
                    rpta = "Error al actualizar";
                }
            }
            catch (Exception ex)
            {

                System.Console.Write(ex.Message);
            }

            return rpta;


        }

        public List<Cargo> listarCargo()
        {
            List<Cargo> lista = new List<Cargo>();
            Cargo C;
            SqlDataReader lector;
            try
            {
                cmdCargo.Parameters.Clear();
                cmdCargo.CommandType = CommandType.StoredProcedure;
                cmdCargo.CommandText = "SP_Listar_Cargo";
                cmdCargo.Connection = conn.conectarBD();

                lector = cmdCargo.ExecuteReader();

                while (lector.Read())
                {
                    C = new Cargo();
                    C.IdCargo = (int)(lector[0]);
                    C.Nombre = (string)lector[1];
                    C.Descripcion = (string)lector[2];
                    lista.Add(C);
                }

            }
            catch (Exception ex)
            {

                System.Console.Write(ex.Message);
            }
            return lista;

        }

        public Cargo buscarCargoById(int id)
        {
            Cargo C = new Cargo();
            SqlDataReader lector;
            try
            {
                cmdCargo.Parameters.Clear();
                cmdCargo.CommandType = CommandType.StoredProcedure;
                cmdCargo.CommandText = "SP_BuscarCargoById";
                cmdCargo.Connection = conn.conectarBD();

                {
                    cmdCargo.Parameters.AddWithValue("@pIdCargo", id);

                }
                lector = cmdCargo.ExecuteReader();
                if (lector.Read())
                {
                    C.IdCargo = (int)(lector[0]);
                    C.Nombre = (string)lector[1];
                    C.Descripcion = (string)lector[2];
                }

            }
            catch (Exception ex)
            {

                System.Console.Write(ex.Message);
            }

            return C;
        }

        public int CodCargo()
        {
            int codigo = 0;
            SqlDataReader lector;
            cmdCargo.Parameters.Clear();
            cmdCargo.CommandType = CommandType.StoredProcedure;
            cmdCargo.CommandText = "SP_Generar_Codigo_Cargo";
            cmdCargo.Connection = conn.conectarBD();

            lector = cmdCargo.ExecuteReader();
            if (lector.Read())
            {
                codigo = (int)lector[0];
            }
            return codigo;


        }

    }
}
